
///
/// This class handles the connection between the client and the server stub
/// allocated to the client
///

#ifndef HANDLER_H
#define HANDLER_H

#include "common.h"
#include <string>

class Handler
{

public:
	
	Handler() {}

	Handler(int fd, std::string& fileName, struct sockaddr_storage clientAddr,
				socklen_t clientAddrSize);

	Handler(const Handler& rhs);
	Handler& operator=(const Handler& copy);

	~Handler();

	int HandleMessage(const char *packet, const unsigned int packetLength);

	int SendData();
	
	bool finished;

private:

	int ActOnMessage();

	int SendError(ERROR_TYPE err, const char* msg);


	int mSockFD;

	char mMsgBuf[MSG_BUF_SIZE];

	char* mDataBuf;
	unsigned int mDataLength;
	unsigned int mDataBufPos;
	unsigned short mBlockNum;


	std::string mFileName;
	FILE*  mFilePtr;

	struct sockaddr_storage mClientAddr;
	socklen_t mClientAddrSize;

	bool mLastBlock;
};

#endif
